<%--
  Created by IntelliJ IDEA.
  User: CY
  Date: 2018/4/9
  Time: 10:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>百度地图</title>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=YECw4heQ9wzy7RKg8OW9QLMsySKzQRan"></script>
    <style>
        html{height:100%}
        body{height:100%;margin:0px;padding:0px}
        .head{height:50px}
        #map{width:100%;height:calc(100% - 50px)}

    </style>
</head>
<body>
<div style="height: 50px;line-height: 50px;text-align: center;">
    <div style="margin-left:50px;float: left">
        <input type="text" id="suggestId" size="20" value="百度" style="width:200px;margin-bottom: 10px;"
               placeholder="请输入地址检索"/>
        <a href="javascript:void(0);" style="" onclick="document.getElementById('suggestId').value=''">清除</a>
    </div>

</div>
<!--<div id="r-result">请输入地址检索：</div>-->
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
<!--用于即时搜索提示-->
<div id="map"></div><!--用于放置地图-->

    </div>
    <script>
        var map = new BMap.Map("map");
        //设置中心点坐标
        var point = new BMap.Point(116.404, 39.915);

        //初始化地图
        /**
         *地图类型：
         *普通地图：BMAP_NORMAL_MAP
         *卫星地图：BMAP_HYBRID_MAP
         */
        var map = new BMap.Map("map", {mapType: BMAP_NORMAL_MAP, minZoom: 1, maxZoom: 18});
        //添加地图类型控件
        map.addControl(new BMap.MapTypeControl({
            mapTypes:[
                BMAP_NORMAL_MAP,
                BMAP_HYBRID_MAP
            ]}));
        //设置地图类型及最小最大级别

        var marker = new BMap.Marker(point); //将点转化成标注点
        map.addOverlay(marker);  //将标注点添加到地图上

        //设置地图级别（1-18）
        map.centerAndZoom(point, 12);
        //开启滚轮缩放地图
        map.enableScrollWheelZoom();

        function G(id) {
            return document.getElementById(id);
        }

        var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
            {
                "input": "suggestId"
                , "location": map
            });

        ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
            G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            setPlace();
        });

        function setPlace() {
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun() {
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(pp, 18);
                map.addOverlay(new BMap.Marker(pp));    //添加标注
            }

            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }
    </script>
</body>
</html>
